
package com.lzx.iteam.contactssearch;

import android.content.res.Resources;
import android.database.Cursor;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import com.lzx.iteam.CloudDialerActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.adapter.ContactEntryAdapter;
//import com.lzx.iteam.calllog.CallLogDBOperation;

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class ResultList extends ContactsSearchList implements OnQueryCompleteListener {

    //private ContactEntryAdapter mSearchAdapter;

    private ContactsSearchEngine mSmartSearch;

    private boolean mListActive = false;

    private boolean mListDisplayed = false;

    public final static int RESULT_ITEM_MENU_ID_SEND_MSG = 1;

    public final static int RESULT_ITEM_MENU_ID_EDIT_BEFORE_CALL = 2;

    public final static int RESULT_ITEM_MENU_ID_ADD_TO_CONTACT = 3;

    public final static int RESULT_ITEM_MENU_ID_VIEW_CONTACT = 4;

    Resources resource;
    
    ResultList(CloudDialerActivity activity, ListView listView) {
        super(activity, listView);
        mSmartSearch = new ContactsSearchEngine(mActivity, this);
//        mSearchAdapter = mActivity.getContactsListAdapter();
        resource = mActivity.getApplicationContext().getResources();
    }

    /**
     * called when activity pauses.
     */
    public void onPause() {
        mListActive = false;
        mListDisplayed = false;
        mSmartSearch.pause();
    }

    @Override
    public void onResume() {
        mListActive = true;
        mListDisplayed = false;
        mSmartSearch.resume();
    }

    @Override
    public void onDestroy() {
//        mSearchAdapter.destroy();
        mSmartSearch.destroy();
    }

    public void onQueryComplete(final Cursor cursor) {
        searchOver();
        if (!mActivity.isFinishing()) {

            mActivity.runOnUiThread(new Runnable() {
                public void run() {
//                    mActivity.changeCursor(cursor, false);
                    //doDisplayResult();
                }
            });
        } else {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    int[] toIntArray(ArrayList<Integer> list) {
        int[] ret = new int[list.size()];
        int i = 0;
        for (Integer e : list)
            ret[i++] = e.intValue();
        return ret;
    }

    public void onQueryComplete(final LinkedHashSet<Long> contactId) {
        if (mStatus == SearchStatus.ENUM_SEARCH_NONE) {
            return;
        }
        searchOver();

        /*final ArrayList<Long> allContactId;
        allContactId = mActivity.getAllContactId();
        final CursorFilterByContactId cursorFilter = new CursorFilterByContactId(mActivity.getContactsListAdapter().getCursor(), allContactId, contactId);
        if (!mActivity.isFinishing()) {
            LogSystem.v(TAG, "main activity is not finishing");

            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    LogSystem.v(TAG, "mactivity changecursor" + contactId.size());
                    mActivity.changeCursor(cursorFilter, false);
                }
            });
        }
        LogSystem.end(TAG);*/
        if (!mActivity.isFinishing()) {
            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    mActivity.changeResult(contactId);
//                    doDisplayResult();
                }
            });
        } 
    }
    
    /**
     * Refresh search result list according to the newly inputed digits
     * 
     * @param keyword inputed digits
     */
    @Override
    public void refresh(String keyword, boolean ifShowPeopleNoPhone) {
        super.refresh(keyword, ifShowPeopleNoPhone);
        mSmartSearch.sendSearchMessage(keyword, ifShowPeopleNoPhone);
    }
    
    @Override
    public void refreshGroup(String groupId,String keyword, boolean ifShowPeopleNoPhone) {
        super.refreshGroup(groupId,keyword, ifShowPeopleNoPhone);
        mSmartSearch.sendSearchMessage(groupId,keyword, ifShowPeopleNoPhone);
    }
    

    /**
     * Do display searched result list, when search query is completed
     */
    /*
    private void doDisplayResult() {
        if (!mListActive || mListDisplayed) {
            return;
        }

        mListView.setVisibility(View.VISIBLE);
        mListView.setAdapter(mSearchAdapter);
        View view = mInflater.inflate(R.layout.item_composer_header, mListView, false);
        //mListView.setPinnedHeaderView(view);
//        mListView.invalidate();
        mListDisplayed = true;
    }
	*/
    
    public ContactsSearchEngine getSmartSearchEngineForTest() {
        return mSmartSearch;
    }

    

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
       
    }

	@Override
	public void onQueryComplete(String groupId, final LinkedHashSet<Long> contactId) {
		// TODO Auto-generated method stub
        if (mStatus == SearchStatus.ENUM_SEARCH_NONE) {
            return;
        }
        searchOver();

        /*final ArrayList<Long> allContactId;
        allContactId = mActivity.getAllContactId();
        final CursorFilterByContactId cursorFilter = new CursorFilterByContactId(mActivity.getContactsListAdapter().getCursor(), allContactId, contactId);
        if (!mActivity.isFinishing()) {
            LogSystem.v(TAG, "main activity is not finishing");

            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    LogSystem.v(TAG, "mactivity changecursor" + contactId.size());
                    mActivity.changeCursor(cursorFilter, false);
                }
            });
        }
        LogSystem.end(TAG);*/
        if (!mActivity.isFinishing()) {
            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    mActivity.changeResultGroup(contactId);
//                    doDisplayResult();
                }
            });
        } 
		
	}
}
